//
//  NonMemberOrderController.m
//  OrderMeal
//
//  Created by 周浩 on 14-3-20.
//  Copyright (c) 2014年 周浩. All rights reserved.
//

#import "NonMemberOrderController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIView+Toast.h"
#import "CustomIOS7AlertView.h"
@interface NonMemberOrderController (){
    NSString *warning;
    NSArray *pickerData;
}
-(BOOL)checkInput;
@end

@implementation NonMemberOrderController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [self.tableView setTableFooterView:view];
    [self getOrderTimeList];
}

-(void)orderWithGoodsid:(NSString *)goodsid
              goodsNums:(NSString *)goodsNums
               lastName:(NSString *)lastName
                    sex:(NSString *)sex
                  phone:(NSString *)phone
             buildingID:(NSString *)buildingID
                address:(NSString *)address
                   time:(NSString *)time
              IPAddress:(NSString *)IPAddress
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    NSDictionary *parameters=@{
                               @"goodsids"   : goodsid,
                               @"goodsNums" : goodsNums,
                               @"lastName"  : lastName,
                               @"sex"       : sex,
                               @"phone"     : phone,
                               @"buildingID": buildingID,
                               @"address"   : address,
                               @"time"      : time,
                               @"IPAddress" : IPAddress,
                               };
    [manager GET:quickorderadd_url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber *status=responseObject[@"success"];
        if ([status isEqualToNumber:@1]) {
            [self.view makeToast:@"下单成功" duration:2.0 position:@"center"];
        }else{
            NSString *msg=responseObject[@"msg"];
            [self.view makeToast:msg duration:2.0 position:@"center"];
        }
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         [self.view makeToast:@"出错" duration:2.0 position:@"center"];
    }];

 }

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(BOOL)checkInput
{
    if ([self.lastNameField.text length]<2) {
        warning =@"昵称至少2位";
        return NO;
    }
    NSArray *sexArray = @[@"男",@"女"];
    if (![sexArray containsObject:self.sexField.text]) {
        warning=@"请填写正确的性别";
        return NO;
    }
    if (![self.phoneField.text length]) {
        warning=@"请填写联系电话";
        return NO;
    }
    
    if (![self.buildingField.text length]) {
        warning=@"请填写送餐大厦";
        return NO;
    }
    if (![self.addressField.text length]) {
        warning=@"请填写房间号";
        return NO;
    }
    if (![self.ipField.text length]) {
        warning=@"请填写Ip地址";
        return NO;
    }
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)submit:(UIButton *)sender {
    [self checkInput];
    NSString *sex = @"男";
    if ([self.sexField.text isEqualToString:@"男"]) {
        sex = @"1";
    }else if ([self.sexField.text isEqualToString:@"女"]){
        sex = @"2";
    }
    if ([self checkInput]) {
        for (NSString *goodsidStr in self.goodsid) {
            NSUInteger index = [self.goodsid indexOfObject:goodsidStr];
            [self orderWithGoodsid:goodsidStr
                         goodsNums:self.goodsNum[index]
                          lastName:self.lastNameField.text
                               sex:sex
                             phone:self.phoneField.text
                        buildingID:self.buildingField.text
                           address:self.addressField.text
                              time:pickerData[[self.timePicker selectedRowInComponent:0]][@"id"]
                         IPAddress:self.ipField.text];
        }
           }else{
        [self.view makeToast:warning duration:2.0 position:@"center"];
    }
    }

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

-(void)getOrderTimeList{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:ordertime_url parameters:nil success:^(AFHTTPRequestOperation *operation,id responseObject){
        if ([responseObject[@"success"] isEqualToNumber:@1]) {
            pickerData = responseObject[@"retData"];
            [self.timePicker reloadAllComponents];
        }
    }failure:^(AFHTTPRequestOperation *operation,NSError *error){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"消息" message:@"网络错误" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [pickerData count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return pickerData[row][@"name"];
}

- (IBAction)pickTime:(id)sender {
    
    CustomIOS7AlertView *alert = [[CustomIOS7AlertView alloc]init];
    UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 212)];
    self.timePicker = [[UIPickerView alloc]initWithFrame:CGRectMake(25, 25, 150, 162)];
    self.timePicker.delegate =self;
    self.timePicker.dataSource = self;
    [demoView addSubview:self.timePicker];
    [alert setContainerView:demoView];
    [alert setButtonTitles:@[@"确认"]];
    [alert show];
    [alert setOnButtonTouchUpInside:^(CustomIOS7AlertView *alertView, int buttonIndex) {
        NSInteger row = [self.timePicker selectedRowInComponent:0];
        [self.timeBTN setTitle:pickerData[row][@"name"] forState:UIControlStateNormal];
    }];
}

@end
